package io.github.talelin.latticy.controller.v1;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.github.talelin.core.annotation.GroupRequired;
import io.github.talelin.core.annotation.PermissionMeta;
import io.github.talelin.core.annotation.PermissionModule;
import io.github.talelin.latticy.common.util.PageUtil;
import io.github.talelin.latticy.dto.order.CreateOrderDTO;
import io.github.talelin.latticy.dto.order.OrderPageDTO;
import io.github.talelin.latticy.dto.order.PageListDTO;
import io.github.talelin.latticy.dto.order.TakeDownPageListDTO;
import io.github.talelin.latticy.model.oms.OrderDO;
import io.github.talelin.latticy.service.oms.OrderService;
import io.github.talelin.latticy.vo.*;
import io.github.talelin.latticy.vo.oms.OrderDetailVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Positive;
import java.util.List;

/**
 * @author generator@wulongbo
 * @since 2022-06-28
 */
@RestController
@RequestMapping("/v1/order")
@PermissionModule(value = "订单管理")
@Validated
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 仓库列表
     */
    @GetMapping("/getWarehouses")
    @GroupRequired
    @PermissionMeta(value = "仓库列表")
    public FindResultVO getOmsWarehouseById() {
        return FindResultVO.success(orderService.getOmsWarehouseById());
    }

    /**
     * 创建订单
     */
    @PostMapping("")
    @GroupRequired
    @PermissionMeta(value = "创建订单")
    public CreatedVO create(@RequestBody @Valid CreateOrderDTO dto) {
        orderService.createOrder(dto);
        return new CreatedVO();
    }

    @PutMapping("/{orderNo}")
    @GroupRequired
    @PermissionMeta(value = "修改订单")
    public UpdatedVO update(@PathVariable @Positive(message = "{order-no.not-empty}") Long orderNo, @RequestBody @Valid CreateOrderDTO dto) {
        orderService.updateOrderByOrderNo(orderNo, dto);
        return new UpdatedVO();
    }

    /**
     * 发货审核
     */
    @PostMapping("/approve/{orderNo}")
    @GroupRequired
    @PermissionMeta(value = "发货审核")
    public UpdatedVO approve(@PathVariable @Positive(message = "{order-no.not-empty}") Long orderNo) {
        orderService.approveOrder(orderNo);
        return new UpdatedVO();
    }

    /**
     * 订单作废
     */
    @PostMapping("/cancel/{orderNo}")
    @GroupRequired
    @PermissionMeta(value = "作废订单")
    public UpdatedVO cancel(@PathVariable @Positive(message = "{order-no.not-empty}") Long orderNo) {
        orderService.cancelOrder(orderNo);
        return new UpdatedVO();
    }

    /**
     * 订单恢复草稿
     */
    @PostMapping("/recovery/{orderNo}")
    @GroupRequired
    @PermissionMeta(value = "订单恢复草稿")
    public UpdatedVO recovery(@PathVariable @Positive(message = "{order-no.not-empty}") Long orderNo) {
        orderService.recoveryOrder(orderNo);
        return new UpdatedVO();
    }

    @DeleteMapping("/{orderNo}")
    @GroupRequired
    @PermissionMeta(value = "删除订单")
    public DeletedVO delete(@PathVariable @Positive(message = "{order-no.not-empty}") Long orderNo) {
        orderService.deleteByOrderNo(orderNo);
        return new DeletedVO();
    }

    @GetMapping("{orderNo}")
    @GroupRequired
    @PermissionMeta(value = "查询订单")
    public OrderDetailVO get(@PathVariable(value = "orderNo") @Positive(message = "{order-no.not-empty}") Long orderNo) {
        return orderService.getByOrderNo(orderNo);
    }

    @GetMapping("/page")
    @GroupRequired
    @PermissionMeta(value = "订单列表")
    public PageResponseVO<OrderDO> page(OrderPageDTO orderPageDTO) {
        IPage<OrderDO> iPage = orderService.getOrdersPage(orderPageDTO);
        return PageUtil.build(iPage);
    }


    /*======================================================feign=========================================================*/

    @PostMapping("/takeDownPageList")
    public FindResultVO takeDownPageList(@RequestBody @Validated TakeDownPageListDTO pageListDTO){
        return orderService.takeDownPageList(pageListDTO);
    }

    @GetMapping("/findById/{id}")
    public FindResultVO findById(@PathVariable Long id){
        return orderService.findById(id);
    }


    @GetMapping("/pageList")
    public FindResultVO pageList(PageListDTO pageListDTO){
        return orderService.pageList(pageListDTO);
    }
    @PostMapping("/findByIds")
    public FindResultVO findByIds(@RequestBody List<Long> ids){
        return orderService.findByIds(ids);
    }
}
